package com.itita.weibo.ui;

import java.util.HashMap;

import weibo4andriod.Status;
import weibo4andriod.User;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.NetUtil;
import com.itita.weibo.util.WeiboUtil;

public final class AddCountctivity extends Activity implements IWeiboActivity {
	
	public ProgressDialog pd;
	
    private User user ;
    
	public void init() {
		// 检查网络
		if (WeiboUtil.checkNet(this)) {// 联网正常
			Log.d(" 检查网络", "网络正常");
			if (!MainService.isrun) {
				MainService.isrun = true;
				Intent it = new Intent(this, MainService.class);
				this.startService(it);
			}
			
		}else { // 提示用户网络异常
			Log.d("检查网络", "网络异常");
			MainService.alertNetError(this);
		}
	}
	protected void onResume() {
		super.onResume();
		init();
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//取消标题栏
		WeiboUtil.closeWindowTitle(this);
		
		MainService.addActivity(this);
		
		this.setContentView(R.layout.login);
		
		this.user  =(User)this.getIntent().getSerializableExtra("user");
		
		this.initControlsAndAddControlsEvent();
	}

	public void initControlsAndAddControlsEvent() {

		final AutoCompleteTextView acUser = (AutoCompleteTextView) findViewById(R.id.user);
		final EditText etPass = (EditText) findViewById(R.id.password);
		final Button btLogin = (Button) this.findViewById(R.id.loginButton);
		
		final TextView tvTitle = (TextView) this.findViewById(R.id.textView);
		tvTitle.setVisibility(View.GONE);
//		tvTitle.setText(this.getResources().getString(R.string.app_name));
//		ddd
//		final ImageView imageViewTitle = (ImageView) title.findViewById(R.id.imageViewTitle);
//    	imageViewTitle.setImageResource(R.drawable.comments); 
    	
		Button btnBack = (Button) this.findViewById(R.id.title_bt_left);
//		btnBack.setText("返回");
		btnBack.setBackgroundResource(R.drawable.title_back);
		
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		this.findViewById(R.id.title_bt_right).setVisibility(View.GONE);
		// 设定TextView的标题文本
	
		
//		tvTitle.setVisibility(View.GONE);
		
		//*************************************************
		if(user != null){
			acUser.setText(user.getUsername());
			etPass.setText(user.getPassword());
		}
		
		
		btLogin.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AutoCompleteTextView acUser = (AutoCompleteTextView) findViewById(R.id.user);
				EditText etPass = (EditText) findViewById(R.id.password);
//				
				// 定义登录验证任务
				final HashMap param = new HashMap();
				param.put("user", acUser.getText().toString());
				param.put("pass", etPass.getText().toString());
				final Task loginTask = new Task(Task.TASK_MODIFY_USER_LOGIN, param);
//				final Task loginTask = new Task(Task.TASK_USER_LOGIN, param);
				MainService.newTaskToTaskQueneHead(loginTask);
				
				// 显示进度条
				pd = new ProgressDialog(AddCountctivity.this);
				pd.setMessage("正在登陆...");
				pd.setTitle(AddCountctivity.this.getResources().getString(
						R.string.app_name));
				pd.show();
			}
		});
	}
	public void refresh(Object... param) {
		pd.dismiss();// 进度条消失

		Toast.makeText(this, (String) param[1], 400).show();
		if (((String) param[1]).indexOf("成功") >= 0) {
			final Intent it = new Intent(this, HomeActivity.class);
			this.startActivity(it);
			finish();
		}
	}


}
